package com.roamer.javase.demo.export.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.style.StyleUtil;
import com.roamer.javase.demo.export.vo.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.*;

/**
 * 导出处理
 *
 * @author roamer
 * @version v1.0
 * @date 2020/5/5 17:15
 */

public class ExportService {

    public static void main(String[] args) {
        new ExportService().exportMultiData2();
    }

    public void exportMultiData2() {
        Map<String, ExportModel> exportModels = getMultipartModels();
        ExcelWriter writer = null;
        for (Map.Entry<String, ExportModel> entry : exportModels.entrySet()) {
            ExportModel exportModel = entry.getValue();
            // 初始创建工作簿和sheet
            if (writer == null) {
                writer = ExcelUtil.getWriter("/Users/wangshichang/WorkSpace/JavaProjects/demo/javase-demo/writeTest.xlsx", entry.getKey());
                StyleUtil.setColor(writer.getStyleSet().getHeadCellStyle(), IndexedColors.WHITE, FillPatternType.SOLID_FOREGROUND);
                writer.getStyleSet().getCellStyleForDate().setDataFormat((short) 15);
            } else {
                // 创建并切换sheet
                writer.setSheet(entry.getKey());
            }
            // 设置映射中文表头
            writer.clearHeaderAlias();
            Map<String, String> alias = exportModel.getExportHeaderAlias();
            alias.forEach(writer::addHeaderAlias);
            // 标题
            writer.merge(exportModel.getExportHeaderAlias().size() - 1, exportModel.getExportTitle());
            // 进行跨行跨列操作
            exportModel.addMergedRegion(writer.getSheet());
            // 写入内存
            List<Map<String, Object>> list = exportModel.getDataMapList();
            // 先写入表头
            writer.writeHeadRow(alias.values());
            // 写入数据
            writer.write(list, false);

        }
        if (writer != null) {
            // 导出
            writer.close();
        }
    }

    public void exportMultiData() {

        ExportModel exportModel = getWaterCoolerModel("万达");
        ExcelWriter writer = ExcelUtil.getWriter("/Users/wangshichang/WorkSpace/Projects/demo/javase-demo/writeTest.xlsx", "客流");
        // 设置映射中文表头
        exportModel.getExportHeaderAlias().forEach(writer::addHeaderAlias);
        StyleUtil.setColor(writer.getStyleSet().getHeadCellStyle(), IndexedColors.WHITE, FillPatternType.SOLID_FOREGROUND);
        // 标题
        writer.merge(exportModel.getExportHeaderAlias().size() - 1, exportModel.getExportTitle());
        // 进行跨行跨列操作
        exportModel.addMergedRegion(writer.getSheet());
        // 写入内存
        List<Map<String, Object>> list = exportModel.getDataMapList();
        writer.write(list, true);
        // 导出
        writer.close();
    }

    /**
     * 模拟冷水机模组
     *
     * @param project
     * @return
     */
    public ExportModel getWaterCoolerModel(String project) {
        WaterCoolerModel waterCoolerModel = new WaterCoolerModel();
        for (int i = 0; i < RandomUtil.randomInt(1, 25); i++) {
            WaterCoolerModel.WaterCoolerItem waterCoolerItem = new WaterCoolerModel.WaterCoolerItem();
            waterCoolerItem.setDate(new Date());
            waterCoolerItem.setProject(project);
            waterCoolerItem.setDeviceName("冷水机");
            waterCoolerItem.setTime(LocalTime.of(i + (RandomUtil.randomInt(2) == 0 || i == 23 ? 0 : 1), 0));
            waterCoolerItem.setBrand("长城牌");
            waterCoolerItem.setNameplateLoadFactor(700);
            waterCoolerItem.setStatus("开启");
            waterCoolerItem.setRunningTimeOfDay(180);
            waterCoolerItem.setSumRunningTime(1800);
            waterCoolerItem.setLoadFactor(new BigDecimal("0.7"));
            waterCoolerModel.appendDataItem(waterCoolerItem);
        }
        return waterCoolerModel;
    }

    /**
     * 模拟客流模组
     *
     * @param project
     * @return
     */
    public ExportModel getFlowModel(String project) {
        PassengerFlowModel flowData = new PassengerFlowModel();
        for (int i = 0; i < RandomUtil.randomInt(1, 25); i++) {
            PassengerFlowModel.PassengerFlowItem flowItem = new PassengerFlowModel.PassengerFlowItem();
            flowItem.setDate(new Date());
            flowItem.setTime(LocalTime.of(i + (RandomUtil.randomInt(2) == 0 || i == 23 ? 0 : 1), 0));
            flowItem.setProject(project);
            flowItem.setNumberOfPeople(0);
            flowItem.setDeviceName("客流");
            flowData.appendDataItem(flowItem);
        }
        return flowData;
    }

    /**
     * 模拟多数据模组
     *
     * @return
     */
    public ExportModel getMultipartModel() {
        ExportModel exportModel1 = getWaterCoolerModel("万达");
        ExportModel exportModel2 = getFlowModel("隆基");
        return new MultipartModel(exportModel1, exportModel2);
    }

    /**
     * 模拟多数据模组
     *
     * @return
     */
    public ExportModel getMultipartModelPro() {
        ExportModel exportModel1 = getWaterCoolerModel("万达");
        ExportModel exportModel2 = getFlowModel("隆基");
        ExportModel exportModel3 = getFlowModel("万达");
        return new MultipartProModel(Arrays.asList(exportModel1, exportModel2, exportModel3));
    }

    /**
     * 模拟多数据模组
     *
     * @return
     */
    public Map<String, ExportModel> getMultipartModels() {
        Map<String, ExportModel> map = new LinkedHashMap<>();
        map.put("冷水机组", new MultipartProModel(
                Arrays.asList(new MultipartRowModel(Arrays.asList(getWaterCoolerModel("隆基"),getWaterCoolerModel("隆基"))),getWaterCoolerModel("万达")))
        );
        map.put("客流", new MultipartProModel(
                Arrays.asList(getFlowModel("隆基"), getFlowModel("万达")))
        );
        return map;
    }
}
